package xjtu.se.articlecommunity.user.login;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xjtu.se.articlecommunity.common.*;
import xjtu.se.articlecommunity.user.login.dao.CodeLoginDao;
import xjtu.se.articlecommunity.user.login.dto.CodeLoginDto;
import xjtu.se.articlecommunity.user.login.dto.TelLoginDto;
import xjtu.se.articlecommunity.user.login.dto.ULoginDto;
import xjtu.se.articlecommunity.user.login.impl.ULoginService;
import xjtu.se.articlecommunity.user.register.dao.UserMapper;

import javax.annotation.Resource;
@Slf4j
@RestController
@RequestMapping("/user/login")
public class ULoginAPI {
    @Resource
    ULoginService service;
    @Resource
    UserMapper userMapper;
    @RequestMapping("/passwordLogin")
    public Result login(@RequestBody ULoginDto dto) {

        boolean confirm = service.checkLogin(dto);

        if (confirm) {
            String token = TokenUtils.loginSign(dto.getU_key(), dto.getPassword(), 0);
            return Result.success((Object) token);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS, "密码错误！");

    }


    @Resource
    MailService mailService;
    @Resource
    SMSService smsService;
    @Resource
    CacheService cacheService;
    @Resource
    CodeLoginDao codeLoginDao;


    @GetMapping("/mailcode")
    public Result mailcode(@RequestParam("mail") String mail) {
        if (userMapper.ifUserExist(mail)>0) {
            log.info(mail);
            String mailcode = mailService.sendCode(mail);
            log.info(mailcode);
            cacheService.saveVerificationCode(mail, mailcode);
            return Result.success();
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"该用户不存在！");

    }


    @RequestMapping("/mailcodeLogin")
    public Result Clogin(@RequestBody CodeLoginDto dto){
        if (cacheService.findVerificationCode(dto.getMail()).equals(dto.getMailcode())){
            String password = codeLoginDao.password(dto.getMail());
            System.out.println(password);
            String token = TokenUtils.loginSign(dto.getMail(), password,0);
            return Result.success((Object)token);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"验证码错误！");
    }

    @GetMapping("/telcode")
    public Result telcodecode(@RequestParam("tel") String tel) {
        if (userMapper.ifUserExist(tel)>0) {
            log.info(tel);
            String telcode = smsService.sendVerificationCode(tel);
            log.info(telcode);
            cacheService.saveVerificationCode(tel, telcode);
            return Result.success();
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"该用户不存在！");

    }


    @RequestMapping("/telcodeLogin")
    public Result Tlogin(@RequestBody TelLoginDto dto){
        if (cacheService.findVerificationCode(dto.getTel()).equals(dto.getTelcode())){
            String password = codeLoginDao.password(dto.getTel());
            System.out.println(password);
            String token = TokenUtils.loginSign(dto.getTel(), password,0);
            return Result.success((Object)token);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"验证码错误！");
    }
}
